$(function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var result = Model.order.findMyOrderByState({
        state: 0
    });
    // if (!sessionStorage.getItem('hmdj_stopError')) {
    //     sessionStorage.setItem('hmdj_stopError', true);
    // } else {
    //     if (result.code) {
    //         Common.fn.formatOutput(result);
    //         loading.hide();
    //         return false;
    //     }
    // }
    if (result.code == 1) {
        loading.hide();
    }


    for (var i = 0; i < result.data.length; i++) {
        if (result.ordermessages[i].state = 1) {
            result.ordermessages[i].message = "买家未付款";
        }

        var item = ' <div class="order-list" data-id="' + result.ordermessages[i].id + '">' +
            '        <div class="weui-cell">' +
            '            <div class="weui-cell__bd weui-cell_access shop-name" data-id="' + result.ordermessages[i].id + '">' +
            '                <img src="' + result.users[i].photo + '" class="user-head-image min-head-img">' +
            '                <span>' + result.users[i].nickname + '</span>' +
            '            </div>' +
            '            <div class="weui-cell__ft">' + result.ordermessages[i].message + '</div>' +
            '        </div>' +
            '        <div class="weui-cell order-detail-info" data-id="' + result.ordermessages[i].id + '">' +
            '            <div class="weui-cell__hd"><img src="' + ((result.goods[i].goodsImg.length > 0) ? result.goods[i].goodsImg[0].image : '') + '" class="shop-img"></div>' +
            '            <div class="weui-cell__bd good-name">' + result.goods[i].content + '</div>' +
            '            <div class="weui-cell__ft">' + result.ordermessages[i].count + '*' + result.goods[i].price + '</div>' +
            '        </div>' +
            '    </div>';
        $('.order-container').append(item);
    }
    if (result.data.length == 0) {
        $('.order-container').append('<p style="text-align: center; color: #666;">暂无订单</p>');
    }
    loading.hide();
});

$(document).on('click', '.shop-name,.order-detail-info', function () {
    var orderId = $(this).attr('data-id');
    location.href = 'order_buyer_detail.html?id=' + orderId;
});


$(document).on('click', '.order-btn', function () {
    var orderid = $(this).attr('data-id');
    var goodsid = $(this).attr('goods-id');
    var total_price = $(this).attr('total_price');

    weui.dialog({
        title: '校淘支付',
        content: '￥' + total_price + '',
        className: 'custom-classname',
        buttons: [{
            label: '取消',
            type: 'default',
            onClick: function () {
            }
        }, {
            label: '支付',
            type: 'primary',
            onClick: function () {
                var result2 = Model.order.perUpdateOrder({
                    state: 1,
                    id: orderid,
                    goodsid: ""

                });
                if (result2.code = 1) {
                    weui.toast('支付成功', {
                        duration: 1500,
                        className: 'custom-classname',
                        callback: function () {

                            location.reload();
                        }
                    });
                }

            }
        }]
    });

});
